<?php
namespace app\modules\common\components;

use Eureka\EurekaClient;
use Yii;

class Eureka
{
    private $eurekaDefaultUrl = "";
    private $ip = "";
    private $appName = "iclass";
    private $port = 80;
    private $homePageUrl = "";
    private $status = "UP";


    private $eurekaClient = null;

    private static $instance = null;

    public function __construct($status = "")
    {
        $statusArr = ['UP','DOWN'];
        $this->eurekaDefaultUrl = Yii::$app->params['eureka_server_url'];
        $this->port = Yii::$app->params['server_port'];
        $this->ip = Yii::$app->params['ip_address'];
        $this->homePageUrl = "http://".$this->ip.":".$this->port;

        if (!empty($status)&& in_array($status, $statusArr)) {
            $this->status = $status;
        }

        $this->eurekaClient = new EurekaClient([
            'eurekaDefaultUrl' => $this->eurekaDefaultUrl,
            'hostName' => $this->ip,
            'appName' => $this->appName,
            'ip' =>$this->ip,
            'port' => [$this->port, true],
            'status' => $this->status,
            'homePageUrl' => $this->homePageUrl,
            'statusPageUrl' => $this->homePageUrl,
            'healthCheckUrl' => $this->homePageUrl,
        ]);
    }

    public static function getInstance($status = "")
    {
        if (self::$instance == null) {
            self::$instance = new Eureka($status);
        }
        return self::$instance;
    }

    public function getEurekaClient():? EurekaClient
    {
        return $this->eurekaClient;
    }
}
